package com.tcp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import Model.User;

/*
 * 这个实例模仿用户登陆认证的功能
 * 本类是对客户端的描述
 */
public class TCPClient {
	public static void main(String[] args) {
		try {
			//建立网络套接字,建立请求会被Server的accept方法捕获
			Socket socket = new Socket("127.0.0.1", 8800);
			//从套接字获取输出流
			OutputStream os = socket.getOutputStream();
			
			//初始化JavaBean对象
			User user = new User();
			user.setId(1);
			user.setName("王二小");
			user.setIs_super_man(true);
			
			//用对象输出流包装os,并输出对象到套接字服务端
			ObjectOutputStream oos = new ObjectOutputStream(os);
			oos.writeObject(user);
			
			//关闭输出流
			socket.shutdownOutput();
			
			//接受服务端反馈的文字信息
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String serverMessage = br.readLine();
			while(serverMessage != null){
				System.out.println("Message From Server:" + serverMessage);
				serverMessage = br.readLine();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}
